package com.webserver.controller;

import com.webserver.entity.Article;
import com.webserver.entity.User;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class ArticleController {
    static final long serialVersionUID = 1L;
    private static File ARTICLE = new File("./articles");

    static {
        if (!ARTICLE.exists()) {
            ARTICLE.mkdirs();
        }
    }

    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String author = request.getParameter("author");
        File userFile1 = new File(ARTICLE, title + ".obj");
        if (title == null || content == null || author == null) {
            File file2 = new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file2);
            return;
        }
        if(userFile1.exists()){
            File file2 = new File("./webapps/myweb/article_fail.html");
            response.setContentFile(file2);
            return;
        }
        File file1 = new File("./webapps/myweb/article_success.html");
        response.setContentFile(file1);
        Article article = new Article(title, content, author);
        try (
                FileOutputStream fos = new FileOutputStream(userFile1);
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {

            oos.writeObject(article);
            response.setContentFile(file1);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void showAllUser(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理动态页面...");
        List<Article> articlesList = new ArrayList<>();
        File[] subs = ARTICLE.listFiles(f -> f.getName().endsWith(".obj"));
        for (int i = 0; i < subs.length; i++) {
            File file = subs[i];
            try (
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                Object obj = ois.readObject();
                if (obj instanceof Article) {
                    Article article = (Article) obj;
                    articlesList.add(article);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

            System.out.println(articlesList + "============================");
            System.out.println("处理动态页面完毕！！！！！！！！");
        }

        File file = new File("./userList.html");
        try (PrintWriter pw = new PrintWriter(file, "UTF-8");) {
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>Title</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章列表</h1>");
            pw.println("<table border=\"3\">");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>用户名</td>");
            pw.println("<td>内容</td>");
            pw.println("</tr>");
            for (Article article : articlesList) {
                pw.println("<tr>");
                pw.println("<td>" + article.getTitle() + "</td>");
                pw.println("<td>" + article.getAuthor() + "</td>");
                pw.println("<td>" + article.getContent() + "</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //3.将该页面设置到响应对象中发给浏览器
        response.setContentFile(file);
    }
}

